home *** CD-ROM | disk | FTP | other *** search
/ Champak 50 / Volume 50 - JOGO DISK .iso / Games / moonstonemadness.swf / scripts / __Packages / SideScroller / MovingObject.as < prev    next >
Encoding:
Text File  |  2007-09-27  |  9.3 KB  |  319 lines

  1. class SideScroller.MovingObject extends SideScroller.BasicObject
  2. {
  3.    var bReactToCollisions;
  4.    var bShouldMove;
  5.    var nSpeedX;
  6.    var nSpeedYRegistration;
  7.    var nSpeedYFront;
  8.    var nReturnRightAngleSpeed;
  9.    var nTimeSinceLastGroundHit;
  10.    var nCliffClimbCapacity;
  11.    var nGroundSpeedModificator;
  12.    var oLayer;
  13.    var mcRef;
  14.    static var BOUNCE_LOSS_HORIZONTAL = 8;
  15.    static var ACCELERATION_VERTICAL = 1.2;
  16.    static var FRICTION_HORIZONTAL = 0.4;
  17.    static var MAX_FRONT_SPEED_VERTICAL = 2;
  18.    static var MINIMUM_SPEED_VERTICAL = 0.2;
  19.    static var MINIMUM_SPEED_HORIZONTAL = 0.4;
  20.    static var ABS_MIN_SPEED = 1;
  21.    static var RETURN_RIGHT_ANGLE_ACCELERATION = 0.5;
  22.    static var DEFAULT_CLIFF_CLIMB_CAPACITY = 25;
  23.    static var ANGLE_CHANGE_DIVIDER = 1;
  24.    static var ANGLE_MINIMUM_CHANGE = 0.5;
  25.    static var MAX_ANGLE = 65;
  26.    function MovingObject(__mcRef, __oLayer)
  27.    {
  28.       super(__mcRef,__oLayer);
  29.       this.bReactToCollisions = true;
  30.       this.bShouldMove = true;
  31.       this.nSpeedX = 0;
  32.       this.nSpeedYRegistration = 0;
  33.       this.nSpeedYFront = 0;
  34.       this.nReturnRightAngleSpeed = 0;
  35.       this.nTimeSinceLastGroundHit = 0;
  36.       this.nCliffClimbCapacity = SideScroller.MovingObject.DEFAULT_CLIFF_CLIMB_CAPACITY;
  37.       this.nGroundSpeedModificator = 0;
  38.       this.oLayer.doAddListener(this);
  39.    }
  40.    function doSetRotation(__nRotation, __bRelative)
  41.    {
  42.       if(__bRelative == undefined)
  43.       {
  44.          __bRelative = false;
  45.       }
  46.       if(__bRelative)
  47.       {
  48.          this.mcRef._rotation += __nRotation;
  49.       }
  50.       else
  51.       {
  52.          this.mcRef._rotation = __nRotation;
  53.       }
  54.    }
  55.    function doMove(__nSX, __nSY, __bRelative)
  56.    {
  57.       if(__bRelative == undefined)
  58.       {
  59.          __bRelative = true;
  60.       }
  61.       if(__bRelative)
  62.       {
  63.          this.doMoveHorizontalTo(this.Ref._x + __nSX);
  64.          this.doMoveVerticalTo(this.Ref._y + __nSY);
  65.       }
  66.       else
  67.       {
  68.          this.doMoveHorizontalTo(__nSX);
  69.          this.doMoveVerticalTo(__nSY);
  70.       }
  71.    }
  72.    function doSetSpeeds(__nSX, __nSY, __bRelative)
  73.    {
  74.       if(__bRelative == undefined)
  75.       {
  76.          __bRelative = false;
  77.       }
  78.       if(__bRelative)
  79.       {
  80.          if(__nSX != undefined)
  81.          {
  82.             this.nSpeedX += __nSX;
  83.          }
  84.          if(__nSY != undefined)
  85.          {
  86.             this.nSpeedYRegistration += __nSY;
  87.          }
  88.       }
  89.       else
  90.       {
  91.          if(__nSX != undefined)
  92.          {
  93.             this.nSpeedX = __nSX;
  94.          }
  95.          if(__nSY != undefined)
  96.          {
  97.             this.nSpeedYRegistration = __nSY;
  98.          }
  99.       }
  100.    }
  101.    function doEnterFrame()
  102.    {
  103.       super.doEnterFrame();
  104.       this.doMoveBothDirections();
  105.       this.doCheckObjects();
  106.    }
  107.    function doDestroy()
  108.    {
  109.       this.oLayer.doRemoveListener(this);
  110.       super.doDestroy();
  111.    }
  112.    function get Speeds()
  113.    {
  114.       return {x:this.nSpeedX,y:this.nSpeedYRegistration};
  115.    }
  116.    function get CliffClimbCapacity()
  117.    {
  118.       return this.nCliffClimbCapacity;
  119.    }
  120.    function doMoveBothDirections()
  121.    {
  122.       this.doGroundAffectHorizontalSpeed();
  123.       var _loc2_ = this.nSpeedX + this.nGroundSpeedModificator;
  124.       if(this.bShouldMove)
  125.       {
  126.          if(_loc2_ < SideScroller.MovingObject.ABS_MIN_SPEED)
  127.          {
  128.             _loc2_ = SideScroller.MovingObject.ABS_MIN_SPEED;
  129.          }
  130.       }
  131.       this.doMoveHorizontalTo(this.Ref._x + _loc2_);
  132.       this.doMoveVerticalTo(this.Ref._y + this.nSpeedYRegistration);
  133.    }
  134.    function doMoveHorizontalTo(__nFuturePosition)
  135.    {
  136.       var _loc6_ = false;
  137.       var _loc3_ = false;
  138.       var _loc2_ = new Array();
  139.       var _loc4_ = undefined;
  140.       switch(Library.Utils.MoreMath.getPolarity(this.nSpeedX))
  141.       {
  142.          case 1:
  143.             _loc4_ = "mcHitFront";
  144.             break;
  145.          case -1:
  146.             _loc4_ = "mcHitBack";
  147.       }
  148.       for(var _loc5_ in this.mcRef.mcState)
  149.       {
  150.          if(this.mcRef.mcState[_loc5_]._name.indexOf(_loc4_) != -1)
  151.          {
  152.             _loc2_.push(this.mcRef.mcState[_loc5_]);
  153.          }
  154.       }
  155.       for(_loc5_ in _loc2_)
  156.       {
  157.          if(!_loc3_)
  158.          {
  159.             _loc3_ = this.isHitObject(_loc2_[_loc5_]);
  160.          }
  161.       }
  162.       if(_loc3_ || _loc6_)
  163.       {
  164.          if(this.bReactToCollisions)
  165.          {
  166.             this.nSpeedX = Library.Utils.MoreMath.getReachZero(this.nSpeedX,SideScroller.MovingObject.BOUNCE_LOSS_HORIZONTAL);
  167.             this.doReactCollision();
  168.             this.nSpeedX *= -1;
  169.             __nFuturePosition = this.mcRef._x + this.nSpeedX;
  170.          }
  171.       }
  172.       this.mcRef._x = __nFuturePosition;
  173.    }
  174.    function doMoveVerticalTo(__nFuturePosition)
  175.    {
  176.       if(this.Speeds.y < 0)
  177.       {
  178.          var _loc2_ = new Array();
  179.          var _loc4_ = "mcHitTop";
  180.          var _loc3_ = false;
  181.          for(var _loc5_ in this.mcRef.mcState)
  182.          {
  183.             if(this.mcRef.mcState[_loc5_]._name.indexOf(_loc4_) != -1)
  184.             {
  185.                _loc2_.push(this.mcRef.mcState[_loc5_]);
  186.             }
  187.          }
  188.          for(_loc5_ in _loc2_)
  189.          {
  190.             if(!_loc3_)
  191.             {
  192.                _loc3_ = this.isHitObject(_loc2_[_loc5_]);
  193.             }
  194.          }
  195.          if(_loc3_)
  196.          {
  197.             this.nSpeedYRegistration = 0;
  198.             this.nSpeedYFront = 0;
  199.             __nFuturePosition = this.mcRef._y;
  200.          }
  201.       }
  202.       var _loc6_ = false;
  203.       var _loc8_ = this.oLayer.getFloorAt(this.Ref._x,__nFuturePosition,this);
  204.       if(Math.floor(__nFuturePosition) >= Math.floor(_loc8_) && this.nSpeedYRegistration >= 0)
  205.       {
  206.          _loc6_ = true;
  207.          this.onHitGround();
  208.          this.nSpeedX = Library.Utils.MoreMath.getReachZero(this.nSpeedX,SideScroller.MovingObject.FRICTION_HORIZONTAL);
  209.          __nFuturePosition = _loc8_;
  210.       }
  211.       if(!_loc6_)
  212.       {
  213.          this.nSpeedYRegistration += SideScroller.MovingObject.ACCELERATION_VERTICAL;
  214.          if(Math.abs(this.nSpeedYRegistration) < SideScroller.MovingObject.MINIMUM_SPEED_VERTICAL)
  215.          {
  216.             this.nSpeedYRegistration = 0;
  217.          }
  218.       }
  219.       else
  220.       {
  221.          this.nSpeedYRegistration = 0;
  222.       }
  223.       this.mcRef._y = __nFuturePosition;
  224.       this.doAdjustToGround(_loc6_);
  225.    }
  226.    function doAdjustToGround(__bRegistrationHitGround)
  227.    {
  228.       var _loc3_ = Library.Utils.MoreMath.getBoundsCenter(this.mcRef.mcState.mcPointRear.getBounds(this.oLayer.Ref));
  229.       var _loc7_ = this.oLayer.getFloorAt(_loc3_.x,_loc3_.y,this);
  230.       var _loc5_ = undefined;
  231.       var _loc6_ = undefined;
  232.       if(_loc7_ > _loc3_.y)
  233.       {
  234.          if(__bRegistrationHitGround)
  235.          {
  236.             this.nSpeedYFront += SideScroller.MovingObject.ACCELERATION_VERTICAL;
  237.          }
  238.          else
  239.          {
  240.             this.nSpeedYFront = 0;
  241.          }
  242.          _loc5_ = _loc3_.y + this.nSpeedYFront;
  243.          _loc6_ = false;
  244.       }
  245.       else
  246.       {
  247.          _loc5_ = _loc7_;
  248.          _loc6_ = true;
  249.          this.nSpeedYFront = 0;
  250.       }
  251.       var _loc2_ = undefined;
  252.       _loc2_ = Math.round(Library.Utils.MoreMath.getAngle(Math.round(_loc3_.x),Math.round(_loc5_),Math.round(this.mcRef._x),Math.round(this.mcRef._y)));
  253.       if(!__bRegistrationHitGround && !_loc6_)
  254.       {
  255.          this.nTimeSinceLastGroundHit = this.nTimeSinceLastGroundHit + 1;
  256.          if(this.nTimeSinceLastGroundHit > 5)
  257.          {
  258.             this.nReturnRightAngleSpeed += SideScroller.MovingObject.RETURN_RIGHT_ANGLE_ACCELERATION;
  259.             _loc2_ = Library.Utils.MoreMath.getReachZero(_loc2_,this.nReturnRightAngleSpeed);
  260.          }
  261.       }
  262.       else
  263.       {
  264.          this.nReturnRightAngleSpeed = 0;
  265.          this.nTimeSinceLastGroundHit = 0;
  266.       }
  267.       if(Math.abs(_loc2_) > SideScroller.MovingObject.MAX_ANGLE)
  268.       {
  269.          _loc2_ = Library.Utils.MoreMath.getPolarity(_loc2_) * SideScroller.MovingObject.MAX_ANGLE;
  270.       }
  271.       var _loc8_ = _loc2_ - this.mcRef._rotation;
  272.       var _loc4_ = _loc8_ / SideScroller.MovingObject.ANGLE_CHANGE_DIVIDER;
  273.       if(Math.abs(_loc4_) < SideScroller.MovingObject.ANGLE_MINIMUM_CHANGE)
  274.       {
  275.          _loc4_ = 0;
  276.       }
  277.       this.mcRef._rotation += _loc4_;
  278.    }
  279.    function doGroundAffectHorizontalSpeed()
  280.    {
  281.       var _loc2_ = 0;
  282.       switch(Library.Utils.MoreMath.getPolarity(this.mcRef._rotation))
  283.       {
  284.          case 1:
  285.             _loc2_ = 1;
  286.             break;
  287.          case -1:
  288.             _loc2_ = -1;
  289.       }
  290.       this.nGroundSpeedModificator = _loc2_ * (Math.pow(Math.abs(this.mcRef._rotation),1.2) * 0.051);
  291.    }
  292.    function isHitObject(__mcHitReference)
  293.    {
  294.       var _loc4_ = Library.Utils.MoreMath.getBoundsCenter(__mcHitReference.getBounds(this.oLayer.Ref));
  295.       var _loc3_ = this.oLayer.getCollidableObjects(this);
  296.       var _loc6_ = this.oLayer.Ref._x;
  297.       var _loc7_ = this.oLayer.Ref._y;
  298.       var _loc5_ = false;
  299.       var _loc2_ = undefined;
  300.       _loc2_ = 0;
  301.       while(_loc2_ <= _loc3_.length - 1)
  302.       {
  303.          if(_loc3_[_loc2_].Hit.hitTest(_loc4_.x + _loc6_,_loc4_.y + _loc7_,true))
  304.          {
  305.             _loc5_ = true;
  306.             _loc3_[_loc2_].onHit(this);
  307.          }
  308.          _loc2_ = _loc2_ + 1;
  309.       }
  310.       return _loc5_;
  311.    }
  312.    function onHitGround()
  313.    {
  314.    }
  315.    function doReactCollision()
  316.    {
  317.    }
  318. }
  319.